#include <stdio.h>

void copy(char *src, char *dst, int size)
{
    for (int i = 0; i < size; i++)
    {
        *dst = *src;
        src++;
        dst++;
    }
}

int cmp(char *p1, char *p2, int count)
{
    for (int i = 0; i < count; i++)
    {
        if (*p1 != *p2)
        {
            return i;
        }
        p1++;
        p2++;
    }
    return -1;
}

void lfsr(char *pointer, int initSize, int epo, int *key, int keySize)
{
    for (int i = 0; i < epo; i++)
    {
        char new = 0;
        for (int j = 0; j < keySize; j++)
        {
            new ^= *(pointer + *(key + j));
        }
        *(pointer + initSize) = new;
        pointer++;
    }
}

char input[] = "000101010011010110011110011100000101000001111110010111000110001001111111000101110111000110101110100101000110001010010101101110110010010011110010000110010110101010001001100101001100110011100010101110001010101110110110111011110011110100101001001110111111001110001101001000100010000000111001000101111001100110111001111011111011011100001101100001000111010101111001111010100110111110100011000110100111010000000110010011001001011011100101100000010101001111101010000100110001011111111000100100100101011110111000110100010111011001110010110111001000000010001010010000100110101010110000101110100010111101001011100110001010000000000000111001001111110001001101000011000011111100000111111100101110101110111101111001001000011010000111001111011100111010011110100101111011011010111000110011111010001101111010011110110101111101010101101110101110111001100000010101101110100000011001010000101100111010101000101011010100010101100000100000011011010011001100011101110011001101101111101001010110011010110000011000000001011111000110001110000011101101100010101011001101001111010111011000011011011000011001101010100010011000101011001100000111010011001101100000011101101111111001001111011011000000111110101110111000001100001100010101011011110001011010001011010000010100011001010110100110011010111001110111100110101000011110101111110001010110110111100000100100111011001010101000100001001101000111100000011100100101100111101001001111111100001011101011010110011111101001110101101001001111111010011100000000101010101100001011001111000000010000100011001011001101011001101010000011001010111010001110111000011101000110001110110110110000010010001111011110001111110111011110101100011001111001111010000101011010110000101001100110000110010110000101100101001011010011101110010100110111111101111011011111000100100011001111010111110000010111100101001010100001101100101010101100010100011000111110111110001000101011110011111000000010110101111011100001001011101000111000010011011011001001101001011010001110100000101101001001110100110011101111000110111011000001101110";
char idata[2022];
char tmp[2022];

int main(int argc, char *argv[])
{
    if (argc < 2)
    {
        return 0xff;
    }
    int start,end;
    sscanf(argv[1], "%d", &start);
    printf("Begin from %d\n", start);
    if (argc <3)
    {
        end = 400;
    }else
    {
        sscanf(argv[2], "%d", &end);
    }
    printf("End before %d\n", end);
    for (int i = 0; i < 2022; i++)
    {
        idata[i] = input[i] == '1' ? 1 : 0;
    }
    int key[] = {0, 0, 0, 0, 0};
    long long count = 0;
    for (key[0] = start; key[0] < end; key[0]++)
    {
        for (key[1] = key[0] + 1; key[1] < 400; key[1]++)
        {
            for (key[2] = key[1] + 1; key[2] < 400; key[2]++)
            {
                printf("current\t%d\t%d\t%d\tall tries\t%lld\n", key[0], key[1], key[2], count);
                for (key[3] = key[2] + 1; key[3] < 400; key[3]++)
                {
                    for (key[4] = key[3] + 1; key[4] < 400; key[4]++)
                    {
                        count++;
                        copy(idata, tmp, 400);
                        lfsr(tmp, 400, 1622, key, 5);
                        if (cmp(tmp, idata, 2022) == -1)
                        {
                            printf("\nKey is %d %d %d %d %d\n", key[0], key[1], key[2], key[3], key[4]);
                            return 0;
                        }
                    }
                }
            }
        }
    }
}